{% extends "base.html" %}

{% block title %}用户详情 - 后台管理系统{% endblock %}

{% block content %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">用户详情</h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        <div class="btn-group me-2">
            <a href="{{ url_for('users') }}" class="btn btn-sm btn-outline-secondary">
                <i class="fas fa-arrow-left"></i> 返回列表
            </a>
        </div>
    </div>
</div>

{% if user %}
    <div class="row">
        <!-- 基本信息 -->
        <div class="col-md-6 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">基本信息</h5>
                </div>
                <div class="card-body">
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>用户ID:</strong></div>
                        <div class="col-sm-8">{{ user[0] }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>用户名:</strong></div>
                        <div class="col-sm-8">{{ user[1] }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>手机号:</strong></div>
                        <div class="col-sm-8">{{ user[2] or '-' }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>邮箱:</strong></div>
                        <div class="col-sm-8">{{ user[3] or '-' }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>用户类型:</strong></div>
                        <div class="col-sm-8">
                            {% if user[4] == 0 %}
                                <span class="badge bg-primary">普通用户</span>
                            {% elif user[4] == 1 %}
                                <span class="badge bg-warning">商家</span>
                            {% elif user[4] == 2 %}
                                <span class="badge bg-danger">禁用用户</span>
                            {% else %}
                                <span class="badge bg-secondary">未知</span>
                            {% endif %}
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>年龄:</strong></div>
                        <div class="col-sm-8">{{ user[7] or '-' }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>性别:</strong></div>
                        <div class="col-sm-8">{{ user[8] or '-' }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>城市:</strong></div>
                        <div class="col-sm-8">{{ user[15] or '-' }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>生日:</strong></div>
                        <div class="col-sm-8">{{ user[18].strftime('%Y-%m-%d') if user[18] else '-' }}</div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 统计信息 -->
        <div class="col-md-6 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">统计信息</h5>
                </div>
                <div class="card-body">
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>积分:</strong></div>
                        <div class="col-sm-8">
                            <span class="badge bg-success fs-6">{{ user[13] or 0 }}</span>
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>等级:</strong></div>
                        <div class="col-sm-8">{{ user[19] or 0 }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>获赞数:</strong></div>
                        <div class="col-sm-8">{{ user[11] or 0 }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>粉丝数:</strong></div>
                        <div class="col-sm-8">{{ user[12] or 0 }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>状态:</strong></div>
                        <div class="col-sm-8">
                            {% if user[20] == '开启' %}
                                <span class="badge bg-success">{{ user[20] }}</span>
                            {% elif user[20] == '关闭' %}
                                <span class="badge bg-warning">{{ user[20] }}</span>
                            {% else %}
                                <span class="badge bg-danger">{{ user[20] }}</span>
                            {% endif %}
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>注册时间:</strong></div>
                        <div class="col-sm-8">{{ user[22].strftime('%Y-%m-%d %H:%M:%S') if user[22] else '-' }}</div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4"><strong>最后登录:</strong></div>
                        <div class="col-sm-8">{{ user[23].strftime('%Y-%m-%d %H:%M:%S') if user[23] else '从未登录' }}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 个性签名 -->
    {% if user[9] %}
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">个性签名</h5>
                </div>
                <div class="card-body">
                    <p class="mb-0">{{ user[9] }}</p>
                </div>
            </div>
        </div>
    </div>
    {% endif %}
    
    <div class="row">
        <!-- 积分记录 -->
        <div class="col-md-6 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">最近积分记录</h5>
                </div>
                <div class="card-body">
                    {% if points_log %}
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>变动</th>
                                        <th>变动前</th>
                                        <th>变动后</th>
                                        <th>原因</th>
                                        <th>时间</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for log in points_log %}
                                    <tr>
                                        <td>
                                            {% if log[0] > 0 %}
                                                <span class="text-success">+{{ log[0] }}</span>
                                            {% else %}
                                                <span class="text-danger">{{ log[0] }}</span>
                                            {% endif %}
                                        </td>
                                        <td>{{ log[1] }}</td>
                                        <td>{{ log[2] }}</td>
                                        <td>{{ log[4] or '-' }}</td>
                                        <td>{{ log[5].strftime('%m-%d %H:%M') if log[5] else '-' }}</td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <p class="text-muted mb-0">暂无积分记录</p>
                    {% endif %}
                </div>
            </div>
        </div>
        
        <!-- 订单记录 -->
        <div class="col-md-6 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">最近订单记录</h5>
                </div>
                <div class="card-body">
                    {% if orders %}
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>订单号</th>
                                        <th>金额</th>
                                        <th>积分</th>
                                        <th>状态</th>
                                        <th>时间</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for order in orders %}
                                    <tr>
                                        <td>{{ order[0][-8:] }}...</td>
                                        <td>¥{{ "%.2f"|format(order[1]) }}</td>
                                        <td>{{ order[2] }}</td>
                                        <td>
                                            {% if order[4] == 0 %}
                                                <span class="badge bg-warning">待支付</span>
                                            {% elif order[4] == 1 %}
                                                <span class="badge bg-success">已支付</span>
                                            {% elif order[4] == 2 %}
                                                <span class="badge bg-danger">支付失败</span>
                                            {% elif order[4] == 3 %}
                                                <span class="badge bg-info">已退款</span>
                                            {% else %}
                                                <span class="badge bg-secondary">已关闭</span>
                                            {% endif %}
                                        </td>
                                        <td>{{ order[6].strftime('%m-%d %H:%M') if order[6] else '-' }}</td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <p class="text-muted mb-0">暂无订单记录</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% else %}
    <div class="alert alert-danger" role="alert">
        <i class="fas fa-exclamation-triangle me-2"></i>
        用户不存在或已被删除
    </div>
{% endif %}
{% endblock %}